package com.lihao.wanandroid.ui

import android.content.Intent
import android.os.Bundle
import com.lihao.jetpackcore.base.BaseViewModel
import com.lihao.jetpackcore.ext.util.dp2px
import com.lihao.jetpackcore.ext.view.gone
import com.lihao.jetpackcore.ext.view.visible
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseActivity
import com.lihao.wanandroid.app.ext.setPageListener
import com.lihao.wanandroid.app.util.CacheUtils
import com.lihao.wanandroid.app.weight.banner.SplashViewHolder
import com.lihao.wanandroid.ui.main.MainActivity
import com.zhpan.bannerview.BannerViewPager
import kotlinx.android.synthetic.main.activity_splash.*

class SplashActivity : BaseActivity<BaseViewModel>() {

    private lateinit var mViewPager: BannerViewPager<String, SplashViewHolder>

    private var resList = arrayOf("玩", "安卓")

    override fun layoutId(): Int = R.layout.activity_splash

    override fun initView(savedInstanceState: Bundle?) {
        mViewPager = findViewById(R.id.navViewPager)
        if (shareViewModel.isFirst.value) {
            ivLogo.gone()
            mViewPager.setHolderCreator { SplashViewHolder() }
                .setIndicatorMargin(0, 0, 0, this@SplashActivity.dp2px(24))
                .setPageListener {
                    if (it == resList.size - 1) {
                        btnJoin.visible()
                    } else {
                        btnJoin.gone()
                    }
                }
            mViewPager.create(resList.toList())
            btnJoin.setOnClickListener {
                toMain()
            }
        } else {
            ivLogo.visible()
            ivLogo.postDelayed({
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                finish()
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }, 300)
        }
    }

    private fun toMain() {
        shareViewModel.isFirst.value = false
        CacheUtils.setFirst(false)
        startActivity(Intent(this@SplashActivity, MainActivity::class.java))
        finish()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
    }

}